import { createI18n } from 'vue-i18n'
import { Locale } from 'vant'
import enUS from 'vant/es/locale/lang/en-US'
import zhCN from 'vant/es/locale/lang/zh-CN'
import koKR from 'vant/es/locale/lang/ko-KR'
import jaJP from 'vant/es/locale/lang/ja-JP'
import ruRU from 'vant/es/locale/lang/ru-RU'
// import idID from 'vant/es/locale/lang/id-ID'
import enLocale from './lang/en_us'
import zhLocale from './lang/zh_cn'
import koLocale from './lang/ko_kr'
import jaLocale from './lang/ja_jp'
import ruLocale from './lang/ru_ru'
// import idLocale from './lang/id_id'

const CreateI18n = createI18n as any
const messages = {
    zh: {
        ...zhCN,
        ...zhLocale
    },
    en: {
        ...enUS,
        ...enLocale
    },
    ko: {
        ...koKR,
        ...koLocale
    },
    ja: {
        ...jaJP,
        ...jaLocale
    },
    ru: {
        ...ruRU,
        ...ruLocale
    },
    // id: {
    //     ...idID,
    //     ...idLocale
    // }
}

export function getLanguage() {
    const chooseLanguage = localStorage.getItem('languageStatus')
    if (chooseLanguage) return chooseLanguage
    // if has not choose language 
    // const navigators: any = navigator;
    // const language = (navigators.language || navigators.browserLanguage).toLowerCase()
    // const locales = Object.keys(messages)
    // //跟随浏览器语言
    // for (const locale of locales) {
    //     if (language.indexOf(locale) > -1) {
    //         return locale
    //     }
    // }
    return 'en'
}
const i18n = new CreateI18n({
    warnHtmlMessage: false,
    legacy: false,
    globalInjection: true,
    locale: getLanguage(),  // 设置默认语言
    messages: messages // 设置资源文件对象
})

// 更新vant组件库本身的语言变化，支持国际化
function vantLocales(lang: any) {
    if (lang === 'en') {
        Locale.use(lang, enUS)
    } else if (lang === 'zh') {
        Locale.use(lang, zhCN)
    } else if (lang === 'ko') {
        Locale.use(lang, koKR)
    } else if (lang === 'ja') {
        Locale.use(lang, jaJP)
    } else if (lang === 'ru') {
        Locale.use(lang, ruRU)
    }
}

export { i18n, vantLocales }